package com.pxmeta.app.controller.game;


import cn.dev33.satoken.annotation.SaCheckLogin;
import com.pxmeta.app.controller.game.request.CreateServiceOrderDTO;
import com.pxmeta.app.controller.game.request.ServicePublishDTO;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.common.utils.SecurityUtils;
import com.pxmeta.common.utils.StringUtils;
import com.pxmeta.dynamic.entity.Dynamic;
import com.pxmeta.dynamic.entity.dto.CreateDynamicDTO;
import com.pxmeta.dynamic.entity.vo.ServiceIntroVO;
import com.pxmeta.dynamic.service.IDynamicService;
import com.pxmeta.order.service.IOrderService;
import com.pxmeta.user.entity.User;
import com.pxmeta.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(value = "服务发布",tags = "服务发布")
@RequiredArgsConstructor
@RequestMapping("/service")
public class ServiceController {

    private final IDynamicService dynamicService;
    private final IUserService userService;
    private final IOrderService orderService;


    @SaCheckLogin
    @ApiOperation("发布服务")
    @PostMapping("/publishService")
    public AjaxResult publishService(@RequestBody ServicePublishDTO dto){
        Long userId = SecurityUtils.getUserId();
        CreateDynamicDTO createDynamicDTO = new CreateDynamicDTO();
        createDynamicDTO.setImgList(dto.getImgList());
        createDynamicDTO.setTitle(dto.getTitle());
        createDynamicDTO.setContent(dto.getContent());
        createDynamicDTO.setPrice(dto.getPrice());
        createDynamicDTO.setSkillId(dto.getSkillId());
        createDynamicDTO.setOriginalPrice(dto.getOriginalPrice());
        dynamicService.publish(userId, createDynamicDTO);

        return AjaxResult.success();
    }


    @GetMapping("/getServiceIntro")
    @SaCheckLogin
    @ApiOperation("获取服务介绍")
    public AjaxResult<ServiceIntroVO> getServiceIntro(@RequestParam Long serviceId){

        Long userId = SecurityUtils.getUserId();
        Dynamic dynamic = dynamicService.getById(serviceId);
        Double price = dynamic.getPrice();
        String title = dynamic.getTitle();
        String imgUrls = dynamic.getImgUrls();


        Long authorId = dynamic.getUserId();
        User user = userService.getById(authorId);
        String voice = user.getVoice();

        ServiceIntroVO vo = new ServiceIntroVO();
        vo.setVoice(voice);
        vo.setTitle(title);
        vo.setPrice(price);
        if (StringUtils.isNotBlank(imgUrls)){
            String img = imgUrls.split(",")[0];
            vo.setImg(img);
        }
        Integer status = orderService.getOrderStatus(userId, serviceId);
        vo.setStatus(status);
        return AjaxResult.success(vo);
    }


    @PostMapping("/createOrder")
    @ApiOperation("创建订单")
    @SaCheckLogin
    public AjaxResult createOrder(@RequestBody CreateServiceOrderDTO dto){

        Long userId = SecurityUtils.getUserId();
        Long serviceId = dto.getServiceId();
        orderService.createOrder(userId, serviceId, null,2);
        return AjaxResult.success();
    }



}
